package org.yang.order.food.service.impl;


import org.yang.order.food.common.Msg;
import org.yang.order.food.dao.MerchantMapper;
import org.yang.order.food.entity.Merchant;
import org.yang.order.food.service.MerchantService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商户服务实现
 * @author shichenyang
 * @email gyv12345@163.com
 * @date 2018/12/03
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MerchantServiceImpl implements MerchantService {
    @Resource
    private MerchantMapper merchantMapper;

    @Override
    public Merchant getById(Integer id) {
        return merchantMapper.selectById(id);
    }

    @Override
    public Msg addMerchant(Merchant merchant) {
        int i=  merchantMapper.insert(merchant);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("添加成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("添加失败");
        }
        return msg;
    }

    @Override
    public Msg updateMerchant(Merchant merchant) {
        int i= merchantMapper.updateByParam(merchant);
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("修改成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("修改失败");
        }
        return msg;
    }

    @Override
    public Msg delMerchant(Merchant merchant) {
        int i=merchantMapper.del(merchant.getId());
        Msg msg=new Msg();
        if (i>0){
            msg.setMsg("删除成功");
        }else{
            msg.setCode(-1);
            msg.setMsg("删除失败");
        }
        return msg;
    }

    @Override
    public PageInfo getMerchants(Merchant merchant, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Merchant> merchants=merchantMapper.list(merchant);
        PageInfo info=new PageInfo(merchants);
        return info;
    }
}
